import 'package:flutter_scaffold/base/base_model.dart';
import 'package:flutter_scaffold/base/status_page_mixin.dart';
import 'package:flutter_scaffold/base/status_widget.dart';
import 'package:get/get.dart';
import '../utils/print_util.dart';

///基类
abstract class BaseViewModel<M extends BaseModel> extends FullLifeCycleController with FullLifeCycleMixin, StatusPageMixin {
  late M model;

  launch(Function block, {Function? error}) {
    var future = Future.value(block());
    if (error == null) {
      future.catchError((error) {
        if (error is Response) {
          Log.i("${error.request?.url.toString()} 请求数据发生错误:${error.statusText}");
        } else {
          Log.i("请求数据错误:${error}");
        }
        status?.value = StatusType.error;
      });
    }
    if (error != null) {
      future.catchError(error);
    }
  }

  M initModel();

  @override
  void onInit() {
    super.onInit();
    showStatusLoading();
    model = initModel()..onInit();
  }

  @override
  void onReady() {
    super.onReady();
    model.onReady();
  }

  @override
  void onDetached() {}

  @override
  void onClose() {
    super.onClose();
    model.dispose();
    model.onClose();
  }

  @override
  void onHidden() {
    //with onpause instead of this
  }

  @override
  void onInactive() {
    //with onpause instead of this
  }
}
